package com.jingran.satokennetty.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import org.springframework.stereotype.Component;

/**
 * netty添加解码器
 */
@Component
public class SocketInitializer extends ChannelInitializer<SocketChannel> {
    private static final Integer  MAX_CONTENT_LENGTH = 1024 * 64;


    @Override
    protected void initChannel(SocketChannel socketChannel){
        ChannelPipeline pipeline = socketChannel.pipeline();
        //websocket基于http协议，所以需要http编解码器
        pipeline.addLast(new HttpServerCodec());
        //对httpMessage进行聚合
        pipeline.addLast(new HttpObjectAggregator(MAX_CONTENT_LENGTH));
        // 添加上自己的处理器
        pipeline.addLast(new ChatHandler());
        //websocket 服务器处理的协议，用于给指定的客户端进行连接访问的路由地址
        pipeline.addLast(new WebSocketServerProtocolHandler("/chat",null, false, Integer.MAX_VALUE, false));
    }
}
